using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.CMS;
using Deepcode.Flux.Core.Systems.CMS.DataTaxonomy;

public partial class Private_CMS_taxonomy_actions : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region Permission Code Override
	public override string RequiredPermissionCode { get { return "FluxDTAccess"; } }
	#endregion
	#region ViewState and Properties
	/// <summary>
	/// Store the ID of the DT node that is selected
	/// </summary>
	protected int NodeID
	{
		get
		{
			object o = ViewState["CID"];
			if( o == null ) return -1;
			return (int) o;
		}
		set
		{
			ViewState["CID"] = value;
		}
	}
	#endregion
	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			// Get the node from the URL
			this.NodeID = GetAttribute("parentid", -1);

			// See if we have a refresh event being passed in
			bool refreshFrames = (GetAttribute("refresh", "") != "");
			if( refreshFrames ) this.ReloadFrames();

			// Load the node 
			DataTaxonomyTree tree = new DataTaxonomyTree();
			DataTaxonomyNode n = tree.GetByID( NodeID );
			if (n == null)
			{
				NodeID = -1;
				txtSelectedNode.Text = "Root";
				txtNodePath.Text = "/";
				
				actAddNew.Enabled = true;
				actDelete.Enabled = false;
				actEdit.Enabled = false;
			}
			else
			{
				txtSelectedNode.Text = n.Name;

				// Work out the full path to the selected node
				string path = "";
				DataTaxonomyNode trav = n;
				while (trav != null)
				{
					path = String.Format("{0}/{1}", trav.Name, path);
					trav = trav.Parent;
				}
				txtNodePath.Text = String.Format("/{0}", path);

				actAddNew.Enabled = true;
				actDelete.Enabled = true;
				actDelete.OnClientClick = "return confirmDelete();";
				actEdit.Enabled = true;
			}
		}
	}
	#endregion
	#region Page Init Event
	/// <summary>
	/// Page initialisation event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Init(object sender, EventArgs e)
	{
		actAddNew.Click += new EventHandler(actAddNew_Click);
		actDelete.Click += new EventHandler(actDelete_Click);
		actEdit.Click += new EventHandler(actEdit_Click);
	}
	#endregion
	#region Utility Methods
	/// <summary>
	/// Utility function to ask the taxonomy tree frame to refresh
	/// </summary>
	protected void ReloadFrames()
	{
		ClientScript.RegisterStartupScript(typeof(Private_CMS_taxonomy_actions),
			"treerefresh",
			String.Format("this.parent.reloadID({0});", this.NodeID),
			true);
	}
	/// <summary>
	/// Utility to reload the tree and actions pane, selecting a new node id
	/// </summary>
	/// <param name="selectID"></param>
	protected void ReloadAll(int selectID)
	{
		ClientScript.RegisterStartupScript(typeof(Private_CMS_taxonomy_actions),
			"allrefresh",
			String.Format("this.parent.reloadID({0});", selectID)
			, true);
	}
	#endregion
	#region Interactions

	/// <summary>
	/// Opens the rename node form which in turn will redirect back to this
	/// actions panel with the refresh attribute set.
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void actEdit_Click(object sender, EventArgs e)
	{
		// Open the rename node form
		Response.Redirect(String.Format("actions/rename.aspx?nodeID={0}", this.NodeID), true);
	}

	
	/// <summary>
	/// Deletes the selected node and attempts to select the parent node, refreshing
	/// both the actions and tree panels.
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void actDelete_Click(object sender, EventArgs e)
	{
		DataTaxonomyTree t = new DataTaxonomyTree();
		DataTaxonomyNode n = t.GetByID( this.NodeID );
		if( n == null ) this.ReloadAll( -1 );

		if (t.DeleteByID(this.NodeID))
			this.ReloadAll(n.FK_ParentID);
	}

	
	/// <summary>
	/// Opens the new category form, which in turn redirects back here with
	/// the new category ID to select it automatically.
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void actAddNew_Click(object sender, EventArgs e)
	{
		// Open the rename node form
		Response.Redirect(String.Format("actions/new.aspx?ParentNodeID={0}", this.NodeID), true);
	}
	#endregion
}
